// pages/lighting/index.js
const app = getApp()
let _timer1 = null;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    device: [],
    deviceId: null,
    open: 0,
    close: 0,
    clickIndex: -2
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let deviceId = options.deviceId
    if (deviceId != null) {
      this.setData({
        deviceId: deviceId
      })
      app.api.getDeviceDetail(deviceId).then(res => {
        if (res.data != null) {
          this.setData({
            device: res.data
          })
          let _open = 0;
          let _close = 0;
          for (let i = 0; i < res.data.sensorList.length; i++) {
            const sensor = res.data.sensorList[i];
            if (sensor.value != null && sensor.value == '1') {
              _open = _open + 1;
            }
          }
          _close = res.data.sensorList.length - _open;
          this.setData({
            open: _open,
            close: _close
          })
          wx.setNavigationBarTitle({
            title: res.data.deviceName
          })
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    let _this = this
    _timer1 = setInterval(function () {
      if (_this.data.deviceId != null) {
        app.api.getDeviceDetail(_this.data.deviceId).then(res => {
          if (res.data != null) {
            _this.setData({
              device: res.data
            })
            let _open = 0;
            let _close = 0;
            for (let i = 0; i < res.data.sensorList.length; i++) {
              const sensor = res.data.sensorList[i];
              if (sensor.value != null && sensor.value == '1') {
                _open = _open + 1;
              }
            }
            _close = res.data.sensorList.length - _open;
            _this.setData({
              open: _open,
              close: _close
            })
            wx.setNavigationBarTitle({
              title: res.data.deviceName
            })
          }
          _this.setData({
            device: res.data
          })
        })
      }
    }, 2500)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    if (_timer1 != null) {
      clearInterval(_timer1)
    }
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    if (_timer1 != null) {
      clearInterval(_timer1)
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  changeStatus(e) {
    let _this = this
    const index = e.currentTarget.dataset.index
    if (this.data.clickIndex == index) {
      return;
    }
    this.setData({
      clickIndex: index
    })
    let sensor = this.data.device.sensorList[index];
    if (sensor.value == '1') {
      sensor.value = '0'
    } else {
      sensor.value = '1'
    }
    let sensors = [];
    sensors.push(sensor)
    app.api.updateSensor(sensors).then(res => {
      wx.showToast({
        title: '修改成功！！',
      })
    })
    setTimeout(function () {
      if (_this.data.clickIndex != null) {
        _this.setData({
          clickIndex: -2
        })
      }
    }, 1500)
  },
  allOpen() {
    if (this.data.clickIndex == -1) {
      return;
    }
    this.setData({
      clickIndex: -1
    })
    let sensors = [];
    for (let i = 0; i < this.data.device.sensorList.length; i++) {
      let sensor = this.data.device.sensorList[i];
      sensor.value = '1'
      sensors.push(sensor)
    }
    app.api.updateSensor(sensors).then(res => {
      wx.showToast({
        title: '修改成功！！',
      })
    })
    let _this = this;
    setTimeout(function () {
      if (_this.data.clickIndex != null) {
        _this.setData({
          clickIndex: -2
        })
      }
    }, 1500)
  },
  allClose() {
    console.log("lighting");
    if (this.data.clickIndex == -1) {
      return;
    }
    this.setData({
      clickIndex: -1
    })
    let sensors = [];
    for (let i = 0; i < this.data.device.sensorList.length; i++) {
      let sensor = this.data.device.sensorList[i];
      sensor.value = '0'
      sensors.push(sensor)
    }
    app.api.updateSensor(sensors).then(res => {
      wx.showToast({
        title: '修改成功！！',
      })
    })
    let _this = this;
    setTimeout(function () {
      if (_this.data.clickIndex != null) {
        _this.setData({
          clickIndex: -2
        })
      }
    }, 1500)
  },
})